package  
{
	import flash.display.MovieClip;
	/**
	 * ...
	 * @author patsup
	 */
	public class AchievementObject extends MenuButton 
	{
		private const HILITE_WIDTH:int = 120;
		private const HILITE_HEIGHT:int = 40;
		private var achievDesc:String;
		private var achievName:String;
		private var achievID:int;
		public var achievNum:int;
		
		private var varCheckStr:String;
		private var quantityCheck:int;
		private var comboAchievsToCheck:Vector.<int>;	//int vector specifying achievement id#'s to check
		
		//private var achievCheckFunc:Function;
		private var isAchieved:Boolean = false;
		private var whenAchieved:int;				//game time of when achieved (in milliseconds)
		private var roundAchieved:int;				//round that this achievement was received
		
		private var achievInfoDialog:MenuButton;	//dialog that pops up when achievement is clicked
		
		private var achievHilite:MovieClip;			//highlight around an achievement shows when displayed if achieved
		
		private var mainRef:Main;
		private var achievementsRef:Achievements;
		
		//public function AchievementObject(mR:Main, achievName:String, achievDesc:String, achievCheckFunc:Function, mc:MovieClip = null) 
		public function AchievementObject(mR:Main, achievementsRef:Achievements, achievID:int,
						achievName:String, achievDesc:String, varCheckStr:String, quantityCheck:int, mc:MovieClip = null) 
		{
			mainRef = mR;
			
			this.achievementsRef = achievementsRef;
			this.achievID = achievID;
			this.achievName = achievName;
			this.achievDesc = achievDesc;
			this.varCheckStr = varCheckStr;
			this.quantityCheck = quantityCheck;
			comboAchievsToCheck = new Vector.<int>;	//if there are combinations of achievements to check, use InitComboAchievs to set them
			//this.achievCheckFunc = achievCheckFunc;
			
			achievInfoDialog = new MenuButton(new ImageData(0, 0, "achievement desc", 0xFFFFFF, 800, 480, 0.75), achievInfoDlgInputFunc);
			
			achievHilite = new MovieClip();
			achievHilite.graphics.beginFill(0xFFFF00, 0.5);
			achievHilite.graphics.drawRect(-2, -2, HILITE_WIDTH+4, HILITE_HEIGHT+4);
			achievHilite.graphics.endFill();
			addChild(achievHilite);				//.visible will be set true/false when needed
			
			//the achievement's x,y coordinates will be properly set by an outside function
			super(new ImageData(0, 0, "an achievement"), achievInputFunc, mc);
		}
		
		//InitComboAchievs()
		//call this when created to set the combination of achievements necessary for this,
		//should only be called once
		public function InitComboAchievs(cArray:Array):void {
			for (var i:int = 0; i < cArray.length; i++) {
				comboAchievsToCheck.push(int(cArray[i]));
			}
		}
		public function UpdateAppearance(xCoord:int, yCoord:int):void {
			x = xCoord;
			y = yCoord;
			if (isAchieved) {
				//trace("achievements: " + achievName + " " + width + " " + height);
				achievHilite.visible = true;
			} else {
				achievHilite.visible = false;
			}
			SetButtonLabel(achievName);
		}
		
		//input to the MenuButton parent class
		private function achievInputFunc():void
		{
			//when clicked, an info dialog about this achievement is displayed
			if (isAchieved) {
				achievInfoDialog.SetButtonLabel(achievName + "\n" + achievDesc + 
										"\n\nAchieved in round " + roundAchieved + " after " + whenAchieved/1000 + " seconds");
			} else {
				achievInfoDialog.SetButtonLabel(achievName + "\n" + achievDesc);
			}
			
			mainRef.addChild(achievInfoDialog);
		}
		//input function to pass to the achievement info dialog
		private function achievInfoDlgInputFunc():void
		{
			mainRef.removeChild(achievInfoDialog);	//the info dialog about this achievement is removed
		}
		public function CheckIfAchieved():Boolean {
			if (comboAchievsToCheck.length > 0) {
				//check for combinations of achievements to see if they've been achieved
				trace("Checking achievement ID's for: " + comboAchievsToCheck);
				for each (var _comboID:int in comboAchievsToCheck) { //check if each id was achieved,
					if (!achievementsRef.IsIDAchieved(_comboID)) {	 //if not then this achievement hasn't been
						return false;
					}
				}
				return true;	//we made it thru the whole list and apparently they were all achieved
			} else {
				trace(varCheckStr + " " + achievementsRef[varCheckStr] + "==" + quantityCheck);
				if (achievementsRef[varCheckStr] >= quantityCheck) return true;
			}
			return false;
		}
		public function ResetAchieved():void { isAchieved = false; }
		public function SetAchieved(roundNum:int, gameTime:int):void
		{
			isAchieved = true;
			roundAchieved = roundNum;
			whenAchieved = gameTime;
		}
		public function GetRoundAchieved():int { return roundAchieved; }
		public function GetWhenAchieved():int { return whenAchieved; }
		public function IsAchieved():Boolean { return isAchieved; }
		public function GetDesc():String { return achievDesc; }
		public function GetName():String { return achievName; }
		public function GetID():int { return achievID; }
	}

}